/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_Flywheel.h
 *
 *  Created on: 2022年12月14日
 *      Author: 64435
 */

#ifndef PERIPHERAL_LAYER_INC_LOS_FLYWHEEL_H_
#define PERIPHERAL_LAYER_INC_LOS_FLYWHEEL_H_

#include "LOS_SYS.h"

#define FW_X 0
#define FW_Y 1
#define FW_Z 2
#define FW_S 3

extern uint8_t TMPACK_FWX[26];
extern uint8_t TMPACK_FWY[26];
extern uint8_t TMPACK_FWZ[26];
extern uint8_t TMPACK_FWS[26];

typedef struct LOS_FWSTA_t
{
    uint8_t VAL;    //数据有效性标志，0：有效， 其他值：无效
    uint8_t CMD;    //开关机指令状态,发送开机指令后置1，发送关机指令后置0
    uint8_t PWR;    //电源状态，读取电源遥测，通电为1，断电为0
    uint8_t MSG;    //通信状态，通信正常为0，通信异常为其他值
    uint8_t SWNUM;  //开关号，指示
    uint8_t RBCNT;  //复位次数
    uint8_t REC;    //CAN识别标志位,0:未识别  1：识别
    uint8_t NFR;    //Need For Reset 需要复位标志位，1为需要复位，0为不需要
    uint8_t RBT_F;  //表示单机是否处于重启过程中 0：未处于 1：处于重启过程中
    uint8_t RECOV;  //恢复计数：单机重启完成后，需要等待一段时间才能再次进入重启流程，将该计数置于某一值开始倒计时，倒计时结束后会再次开始判断是否需要重启
    uint16_t FlagF_CNT;         //物理量异常重启计数
    uint16_t FlagT_CNT;         //通信异常重启计数
    uint16_t FlagY_CNT;  
    
    float TM_RPM;   //遥测返回转速值，单位rpm，MSG!=0时该值无意义
    float TM_CRT;   //遥测返回电流值，单位mA ，MSG!=0时该值无意义
    uint8_t TM_MOD; //遥测返回模式，0：默认模式  1：电流模式  2：转速模式  3：力矩模式
    uint8_t TM_CNT; //遥测返回计数值（飞轮负责）
    float TM_LSET;  //遥测返回最后一次设置值
    uint8_t HL_TXF; //HL_前缀表示健康管理相关变量。遥测请求发送完成标志,发送一次后该量置1，接收到回应后置0(TX FINISHED)
    uint8_t HL_WCNT;//等待接收计数器。该计数器在操作系统的一个线程中累加，当HL_TXF为1时该值+1，HL_TXF为0时该值清0，当累加到一定值时系统会判断通信异常，并将MSG置1
    uint32_t TMCNT;
}LOS_FWSTA_t;

typedef struct LOS_FWTEST_t
{
    uint8_t rpm_sel; //操作选择:  0:恢复/不操作  1:改变生效
    float rpm_upt;
}LOS_FWTEST_t;


extern LOS_FWSTA_t STA_FW[4];  //飞轮状态
LOS_FWTEST_t TEST_FW[4];

void LOS_FW_GetState(uint8_t FW_NUM);
void LOS_FW_SetCurrent(uint8_t FW_NUM, float cur);
void LOS_FW_SetAngvel(uint8_t FW_NUM, float angvel);
void LOS_FW_SetTorque(uint8_t FW_NUM, float torque);
void LOS_FW_PWROn(uint8_t FW_NUM);
void LOS_FW_PWROff(uint8_t FW_NUM);
void LOS_CAN_FWXCallBack(LOS_CANMSG_t* can_re);
void LOS_CAN_FWYCallBack(LOS_CANMSG_t* can_re);
void LOS_CAN_FWZCallBack(LOS_CANMSG_t* can_re);
void LOS_CAN_FWSCallBack(LOS_CANMSG_t* can_re);
void LOS_CAN_FWCallBack(uint8_t FW_NUM, LOS_CANMSG_t* can_re);

void LOS_FW_GetState_Dyn(uint8_t FW_NUM); 
#endif /* PERIPHERAL_LAYER_INC_LOS_FLYWHEEL_H_ */
